package org.seanye.com.junitrunnerui.view;

import java.lang.reflect.Method;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.seanye.com.junitrunnerui.junit.IFinder;
import org.seanye.com.junitrunnerui.junit.JavaFinder;
import org.seanye.com.junitrunnerui.junit.Junit3Test;

public class TestsView extends ViewPart {
	public static final String ID = "org.seanye.com.JUnitRunnerUI.view";

	private TreeViewer viewer;

	/**
	 * The content provider class is responsible for providing objects to the
	 * view. It can wrap existing objects in adapters or simply return objects
	 * as-is. These objects may be sensitive to the current input of the view,
	 * or ignore it and always show the same content (like Task List, for
	 * example).
	 */
	class ViewContentProvider implements ITreeContentProvider {

		public Object[] getChildren(Object parentElement) {
			if (parentElement instanceof Junit3Test) {
				Junit3Test tests = (Junit3Test) parentElement;
				return tests.getTestMethods().toArray(new Object[0]);
			}
			return null;
		}

		public Object getParent(Object element) {
			if (element instanceof Method) {
				Method method = (Method) element;
				return method.getDeclaringClass();
			} else
				return null;
		}

		public boolean hasChildren(Object element) {
			if (element instanceof Junit3Test)
				return true;
			return false;
		}

		public Object[] getElements(Object inputElement) {
			IFinder finder = new JavaFinder(
					"d:\\Test\\test.jar",
					new String[] { "D:\\.m2\\repository\\junit\\junit\\3.8.2\\junit-3.8.2.jar" });
			return finder.find().toArray(new Object[0]);
		}

		public void dispose() {

		}

		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

		}
	}

	class ViewLabelProvider  implements
			ILabelProvider {
		public String getText(Object obj) {
			if (obj instanceof Junit3Test) {
				return ((Junit3Test) obj).getCaseKlass().getCanonicalName();
			} else if (obj instanceof Method) {
				return ((Method) obj).getName();
			} else
				return "";
		}

		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}

		public Image getImage(Object obj) {
			if (obj instanceof Junit3Test) {
				return ImageManager.getJunitImage("tsuite.png");
			} else if (obj instanceof Method) {
				return ImageManager.getJunitImage("test.png");
			} else
				return PlatformUI.getWorkbench().getSharedImages().getImage(
						ISharedImages.IMG_OBJ_ELEMENT);
		}

		public void addListener(ILabelProviderListener listener) {
			
		}

		public void dispose() {
			
		}

		public boolean isLabelProperty(Object element, String property) {
			return false;
		}

		public void removeListener(ILabelProviderListener listener) {
			
		}
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setLabelProvider(new ViewLabelProvider());
		viewer.setInput(getViewSite());
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
}